<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
use app\api\model\QrCode as QrCodeModel;
use app\user\model\Language;
use app\user\model\Email;
use PHPMailer\PHPMailer\PHPMailer;
use think\facade\Log;
// 为方便系统核心升级，二次开发中需要用到的公共函数请写在这个文件，不要去修改common.php文件
if (!function_exists('get_languages')) {
    function get_languages(){
        $languages=Language::where('status','=',1)->order('is_default','desc')->field(['name','sign'])->select();
        return $languages;
    }
}

if (!function_exists('get_emails')) {
    function get_emails(){
        $emails=Email::where('status','=',1)->order('id','asc')->field(['id','first_name','last_name','email'])->select();
        return $emails;
    }
}

if (!function_exists('send_email')) {
    function send_email($to_name,$title,$content,$email){
        $mail = new PHPMailer();
        try {
            $mail->CharSet=config('setting.mail.CharSet');
            $mail->Encoding = config('setting.mail.Encoding');
            $mail->SMTPDebug = config('setting.mail.Debug');
            $mail->isSMTP(); // Set mailer to use SMTP
            $mail->SMTPAuth = config('setting.mail.SMTPAuth'); // 启用 SMTP 验证功能
            $mail->SMTPSecure = config('setting.mail.SMTPSecure'); // Enable TLS encryption, `ssl` also accepted
            $mail->Host = config('setting.mail.Host'); // Specify main and backup SMTP servers
            $mail->Port = config('setting.mail.Port'); // SMTP服务器的端口号
            $mail->Username = config('setting.mail.Username'); // SMTP username
            $mail->Password = config('setting.mail.Password'); // SMTP password
            $mail->setFrom(config('setting.mail.From'), config('setting.mail.Name'));
            $mail->AddAddress($email, $to_name); // Add a recipient
            $mail->Subject = $title;
            $mail->Body = $content;
            if($mail->send()){
                return true;
            }else{
                Log::error($mail->ErrorInfo);
                return false;
            }
        }catch (Exception $exception){
            Log::error($mail->ErrorInfo);
            return false;
        }
    }
}

if (!function_exists('async_sned_email')) {
    function async_sned_email($list){
        try{
            $client = new \swoole_client(SWOOLE_SOCK_TCP,SWOOLE_SOCK_SYNC);
            $re=$client->connect(config('swoole_server.host'),config('swoole_server.port'));
            if($re){
                $client->send(json_encode($list));
                $client->close();
                return true;
            }else{
                return false;
            }
        }catch (Exception $exception){
            return false;
        }


    }
}

if (!function_exists('token')) {
    function token($length = 32)
    {
        if (!isset($length) || intval($length) <= 8) {
            $length = 32;
        }
        if (function_exists('random_bytes')) {
            $token = bin2hex(random_bytes($length));
        }
        if (function_exists('mcrypt_create_iv') && phpversion() < '7.1') {
            $token = bin2hex(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM));
        }
        if (function_exists('openssl_random_pseudo_bytes')) {
            $token = bin2hex(openssl_random_pseudo_bytes($length));
        }
        return substr($token, -$length, $length);
    }
}
if (!function_exists('RGBToHex')) {
    function RGBToHex($rgb)
    {
        $regexp = "/^rgb\(([0-9]{0,3})\,\s*([0-9]{0,3})\,\s*([0-9]{0,3})\)/";
        $re = preg_match($regexp, $rgb, $match);
        $re = array_shift($match);
        $hexColor = "#";
        $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
        for ($i = 0; $i < 3; $i++) {
            $r = null;
            $c = $match[$i];
            $hexAr = array();
            while ($c > 16) {
                $r = $c % 16;
                $c = ($c / 16) >> 0;
                array_push($hexAr, $hex[$r]);
            }
            array_push($hexAr, $hex[$c]);
            $ret = array_reverse($hexAr);
            $item = implode('', $ret);
            $item = str_pad($item, 2, '0', STR_PAD_LEFT);
            $hexColor .= $item;
        }
        return $hexColor;
    }
}

if (!function_exists('GetUUID')) {
    function GetUUID()
    {
        $uuid=uniqid('',true);
        $uuid=md5($uuid);
        $res=QrCodeModel::WHERE('uuid','=',$uuid)->count();
        if($res!=0){
            GetUUID();
        }
        //录入
        $expire_time=60;  //1分钟有效期
        $data['uuid']=$uuid;
        $data['expire_time']=date('Y-m-d H:i:s',time()+$expire_time);
        $data['uuid']=$uuid;
        $data['create_time']=$data['update_time']=date('Y-m-d H:i:s');
        QrCodeModel::insert($data);
        return $uuid;
    }
}



